package com.zhou.framework.util;

import com.zhou.framework.config.DbSettingConfig;
import com.zhou.util.*;
import com.zhou.framework.config.PasswordPolicyConfig;
import com.zhou.org.dao.Setting;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.*;

/**
 * 缓存工具
 * @author lang.zhou
 * @date 2022/5/6
 */
@Component
@Slf4j
public class SettingUtil implements ApplicationRunner {
    private static DbSettingConfig config;

    public SettingUtil(DbSettingConfig config) {
        SettingUtil.config = config;
    }

    //private static final boolean CACHE_ENABLE = PropertyUtil.getBooleanValue("app.properties","setting_enum.cache",true);
    public static String getTempDir(){
        String tempDir = SettingUtil.getString("system","temp-dir","");
        if(StringTool.isEmpty(tempDir)){
            tempDir = OsUtil.isLinux() ? "/home/app/temp" : "d:/temp";
        }
        File file = new File(tempDir);
        Assert.isTrue(file.isDirectory() || file.mkdirs(),"系统临时文件夹不存在或无效！");
        return FileUtil.formatFilePath(file.getAbsolutePath());
    }
    public static String getDefaultPassword(){
        return PasswordPolicyConfig.getInstance().getDefaultPassword();
    }

    public static Integer getAdminOrgId(){
        return getInteger("org","admin-role",0);
    }

    public static List<String> getStringArray(String module, String name, List<String> dv){
        String[] value = getString(module, name,"").split(",");
        List<String> val = new ArrayList<>(value.length);
        if(value.length > 0){
            for (String s : value) {
                if(StringTool.isNotBlank(s)){
                    val.add(s);
                }
            }
        }
        return ObjectUtil.isEmpty(val) ? dv : val;
    }

    public static List<Integer> getIntegerArray(String module, String name, List<Integer> dv){
        List<String> val = getStringArray(module,name,new ArrayList<>(0));
        List<Integer> res = new ArrayList<>(val.size());
        if(val.size() > 0){
            for (String s : val) {
                res.add(Integer.parseInt(s));
            }
        }
        return ObjectUtil.isEmpty(res) ? dv : res;
    }

    public static String getString(String module, String name, String dv){
        String value = getString(module, name);
        return value == null ? dv : value;
    }

    public static String getString(String module, String name){
        Setting setting = config.getSetting(module, name);
        return setting == null ? null : setting.getValue();
    }
    public static int getInteger(String module, String name, Integer dv){
        Setting setting = config.getSetting(module, name);
        return setting == null ? dv : NumberUtil.safeToInteger(setting.getValue(),dv);
    }
    public static Integer getInteger(String module, String name){
        Setting setting = config.getSetting(module, name);
        return setting == null ? null : NumberUtil.safeToInteger(setting.getValue(),null);
    }
    public static Boolean getBoolean(String module,String name,Boolean dv){
        Boolean s = getBooleanValue(module, name);
        return s == null ? dv : s;
    }
    public static Boolean getBooleanValue(String module,String name){
        Setting setting = config.getSetting(module, name);
        return setting == null ? null : Boolean.valueOf(setting.getValue());
    }
    public static Map<String,Setting> getSystemSettings(){
        return config.getSettingMap("system");
    }

    public static Map<String,Setting> getMapSetting(String module){
        return config.getSettingMap(module);
    }
    public static Map<String,Object> getSettingMap(String module){
        Map<String, Setting> settingMap = config.getSettingMap(module);
        Map<String,Object> map = new HashMap<>(settingMap.size());
        for (Map.Entry<String, Setting> entry : settingMap.entrySet()) {
            map.put(entry.getKey(), entry.getValue().getValue());
        }
        return map;
    }

    public static void putSetting(Setting setting){
        config.putSetting(setting);
    }
    public static Setting decode(Setting setting){
        if(setting!= null && Objects.equals(setting.getEncrypt(),1)){
            setting.setValue(EncryptUtil.decryptDES(setting.getValue()));
        }
        return setting;
    }
    public static Setting encode(Setting setting){
        if(setting!= null && Objects.equals(setting.getEncrypt(),1)){
            setting.setValue(EncryptUtil.encryptDES(setting.getValue()));
        }
        return setting;
    }


    /**
     * 刷新参数缓存
     */
    public static void refreshCache(){
        config.load();
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        refreshCache();
        log.info("参数缓存完成");
        config.clearSettingLoadCache();
    }
}
